package cn.hx.mall.permissions.controller;

import cn.hx.mall.permissions.entity.ResourceCategory;
import cn.hx.mall.permissions.entity.Resources;
import cn.hx.mall.permissions.service.ResourceCategoryService;
import cn.hx.mall.permissions.service.ResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 后台资源表(Resources)表控制层
 *
 * @author makejava
 * @since 2022-11-02 21:06:02
 */
@Controller
@RequestMapping("/permissions")
public class ResourcesController {
    @Resource
    private ResourceCategoryService resourceCategoryService;
    @Resource
    private ResourcesService resourcesService;

    @GetMapping("/resource")
    public String resource(Model model, Resources resources,PageInfo pageInfo) {
        if (pageInfo.getPageNum()==0){
            pageInfo.setPageNum(1);
        }if(pageInfo.getPageSize()==0){
            pageInfo.setPageSize(10);
        }
        PageHelper.startPage(pageInfo);
        List<Resources> resourcesList = resourcesService.queryAll(resources);
        PageInfo resourcesPageInfo = new PageInfo<>(resourcesList);
        model.addAttribute("pageInfo", resourcesPageInfo);
        List<ResourceCategory> categoryList = resourceCategoryService.queryAll(null);
        model.addAttribute("categoryList", categoryList);
        return "permissions/resource";
    }

    @PostMapping("/updateResources")
    public String updateResources(Resources resources){
        resources.setCreateTime(new Date());
        resourcesService.update(resources);
        System.out.println(resources);
        return "redirect:/permissions/resource";
    }

    @PostMapping("/insertResources")
    public String insertResources(Resources resources) {
        resources.setCreateTime(new Date());
        resourcesService.insert(resources);
        return "redirect:/permissions/resource";
    }

    @PostMapping("/findById")
    @ResponseBody
    public Resources findById(Integer id) {
        return resourcesService.queryById((long) id);
    }

    @GetMapping("/deleteResource/{id}")
    public String deleteResource(@PathVariable Integer id) {
        resourcesService.deleteById((long) id);
        return "redirect:/permissions/resource";
    }


}